﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

namespace DNA.Mvc.jQuery
{
    public class ComboBox<TModel> : RemoteDataBindableContainer<TModel, NavigableView>
        where TModel : class
    {
        public override string TagName
        {
            get
            {
                return "input";
            }
        }

        private HtmlTemplate dropDownTemplate = new HtmlTemplate();

        public HtmlTemplate DropDownTemplate
        {
            get { return dropDownTemplate; }
            set { dropDownTemplate = value; }
        }

        /// <summary>
        /// Gets / Sets the list items.
        /// </summary>
        public IList<NavigableView> Items
        {
            get
            {
                return InnerItems;
            }
            set
            {
                InnerItems = value;
            }
        }

        public override void RenderContent(HtmlTextWriter writer) { }

        public override void RenderEndTag(HtmlTextWriter writer)
        {
            base.RenderEndTag(writer);

            if (string.IsNullOrEmpty(RemoteDataSource))
            {
                string _tag =DropDownTemplate.IsEmpty ? "ul": "div";

                writer.WriteBeginTag(_tag);
                writer.WriteAttribute("id", Id + "_menu");
                writer.WriteAttribute("unselectable", "on");
                writer.WriteAttribute("style", "display:none");
                writer.Write(HtmlTextWriter.TagRightChar);

                if (DropDownTemplate.IsEmpty)
                {
                    foreach (var item in InnerItems)
                    {
                        //item.Template.Content.
                        item.Render(writer);
                    }
                }
                else
                    DropDownTemplate.WriteTo(writer);

                writer.WriteEndTag(_tag);
            }
        }
    }
}
